<template>
  <BasicTable @register="registerTable">
    <template #tableTitle>
      <ButtonGroup>
        <a-button type="primary" @click="handleCreate"> 新增 </a-button>
      </ButtonGroup>
    </template>
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'action'">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              label: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </template>
  </BasicTable>
  <FormModal @register="registerModal" @success="handleSuccess" />
</template>

<script lang="ts">
  import { defineComponent } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { ButtonGroup } from 'ant-design-vue';
  import { useModal } from '/@/components/Modal';
  import FormModal from './FormModal.vue';
  import { getDBPageApi, delDBApi } from '/@/api/database/account';
  import { columns, searchFormSchema } from './account.data';

  export default defineComponent({
    name: 'DatabaseAccount',
    components: { BasicTable, TableAction, FormModal, ButtonGroup },
    setup() {
      const { createMessage } = useMessage();
      const [registerModal, { openModal }] = useModal();

      const [registerTable, { reload }] = useTable({
        api: getDBPageApi,
        columns: columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: false,
        bordered: true,
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          fixed: undefined,
        },
      });

      function handleEdit(record: Recordable) {
        console.log('record', record);
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        // console.log('record', record);
        const { connCode } = record;
        await delDBApi(connCode);
        createMessage.success('操作成功');
        handleSuccess();
      }

      function handleCreate() {
        openModal(true, {
          idupdate: false,
        });
      }

      function handleSuccess() {
        reload();
      }
      return {
        registerTable,
        handleEdit,
        handleDelete,
        handleCreate,
        registerModal,
        handleSuccess,
      };
    },
  });
</script>
<style lang="less" scoped></style>
